package com.sjlh.centurylianhua.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sjlh.centurylianhua.dto.SearchCon;
import com.sjlh.centurylianhua.entity.Depot;
import com.sjlh.centurylianhua.mapper.DepotMapper;
import com.sjlh.centurylianhua.utils.ResultData;
import com.sjlh.centurylianhua.utils.StringUtils;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("/depot")
public class DepotController {
    @Resource
private DepotMapper depotMapper;

    /**
     * 根据地址和仓库名称分页查询
     *
     * */
    @RequestMapping("/getAll")
    public Object getAll(@RequestBody SearchCon searchCon){
        QueryWrapper<Depot> wrapper = new QueryWrapper<>();
        //当条件不为空时，添加where条件
        wrapper.like(StringUtils.isnotNull(searchCon.getName()), "name", searchCon.getName());
        wrapper.like(StringUtils.isnotNull(searchCon.getName2()),"address",searchCon.getName2());
        //没有分页时查询所有模块
        if(searchCon.getPage()==null){
            List<Depot> list = depotMapper.selectList(wrapper);//所有数据
            long count = depotMapper.selectList(wrapper).size();//总条数
            ResultData rs=new ResultData(true,20000,"成功",list,count);
            return rs;
        }else {
            // 封装分页对象
            Page<Depot> page = new Page<Depot>(searchCon.getPage(), searchCon.getLimit());
            Page<Depot> depotPage = depotMapper.selectPage(page, wrapper);
            List<Depot> list=depotPage.getRecords();//所有数据
            long count=depotPage.getTotal();//总条数
            ResultData rs=new ResultData(true,20000,"成功",list,count);
            return rs;
        }
    }
    /**
     *根据仓库名字精确查询仓库
     * */
    public Depot getOne(@RequestBody Depot depot){
        QueryWrapper<Depot> wrapper = new QueryWrapper<>();
        wrapper.eq( "name", depot.getName());
        return depotMapper.selectOne(wrapper);
    }

    /**
     *当名字不重复时添加模块
     * */
    @RequestMapping("/add")
    public Object add(@RequestBody Depot depot){
        //调用getOne查看仓库名是否重复
        Depot d = this.getOne(depot);
        if (d == null) {
            int r=depotMapper.insert(depot);
            if(r==1)
                return new ResultData(true,20000,"添加仓库成功",true,null);
            else
                return new ResultData(false,20001,"添加仓库失败",false,null);
        } else {
            return new ResultData(false,20001,"仓库名已存在",false,null);
        }

    }
    /**
     * 当没有其他仓库名和修改后的模块名不重复时修改仓库
     * */
    @RequestMapping("/set")
    public Object set(@RequestBody Depot depot){
        QueryWrapper<Depot> wrapper = new QueryWrapper<>();
        //添加where条件查询id和当前id不同，名字相同的仓库
        wrapper.eq( "name", depot.getName());
        wrapper.ne("id",depot.getId());
        Depot d = depotMapper.selectOne(wrapper);
        //没有时修改仓库
        if (d == null) {
            int r = depotMapper.updateById(depot);
            if(r==1)
                return new ResultData(true,20000,"修改仓库成功",true,null);
            else
                return new ResultData(false,20001,"修改仓库失败",false,null);
        } else {
            return new ResultData(false,20001,"修改过的仓库名已存在",null,null);
        }

    }
    /**
     *  删除模块
     * */
    @RequestMapping("/del")
    public Object del(@RequestBody Depot depot){
        int rs = depotMapper.deleteById(depot.getId());
        if(rs==1)
            return new ResultData(true,20000,"删除成功",true,null);
        else
            return new ResultData(false,20001,"删除失败",false,null);
    }

}
